﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;

namespace PlantsZombies.Common.Plant
{
    [PlantZombie(SpeciesType.FaShi)]
    public class GameFaShi : PlantObject
    {
        private readonly Random _random = new Random();
        public GameFaShi(ImageObject[] images)
             : base(images, SpeciesType.FaShi, 100, 5, new Size(150, 150))
        {

        }
        protected override void PlayBody(IntPtr hdc)
        {
            var image = _images[_curPlayIndex];
            GDI32.DrawImageTransparenc(hdc, image.BitmapPtr, Location.X + (CellWidth - Size.Width) / 2, Location.Y + (CellHeight - Size.Height) / 2-(int)(40*ZoomY), image.Width, image.Height, Size.Width, Size.Height);
        }
        bool _isAdd = true;
        Tuple<int, int> _curValue = null;
        internal int _bodyType = 0;
        public override unsafe void Move(ListGameObject* ptr)
        {
            if (_curValue == null)
            {
                var n = GetBody();
                _bodyType = n;
                _curValue = GetBody(n);
                _curPlayIndex = _curValue.Item1;
                _isAdd = true;
            }
            else
            {
                if (_bodyType == 6)
                {
                    if (_isAdd)
                    {
                        if (++_curPlayIndex >= _curValue.Item2)
                        {
                            _isAdd = false;
                            _curPlayIndex = _curValue.Item2;
                            List<GameObject> rows=new List<GameObject>();
                            for (int i = 0; i < MaxRow; i++)
                            {
                                for (int j = 0; j < UseMaxColumn; j++)
                                {
                                    ListGameObject* p=  ptr + i * MaxColumn + j;
                                    if (p->Game.Count == 0) continue;
                                    for (int k = 0; k < p->Game.Count; k++)
                                    {
                                        var game= p->Game[k];
                                        if (game.IsZombie())
                                        {
                                            rows.Add(game);
                                        }
                                    }
                                }
                            }
                            if(rows.Count == 0)
                            {
                                Row = _random.Next(MaxRow);
                                Column = _random.Next((UseMaxColumn >> 1) + 1);
                            }
                            else
                            {
                                var gb= rows[_random.Next(rows.Count)];
                                Row = gb.Row;
                                Column = _random.Next(gb.Column > 1 ? gb.Column - 1 : gb.Column);
                            }
                            Location = new Point(StartX + Column * CellWidth, StartY + Row * CellHeight);
                        }
                    }
                    else
                    {
                        if (--_curPlayIndex <= _curValue.Item1)
                        {
                            _isAdd = true;
                            _curPlayIndex = _curValue.Item1;
                            _curValue = null;
                        }
                    }
                }
                else
                {
                    if (_bodyType == 1)
                    {
                        int maxW = StartX + CellWidth * UseMaxColumn;
                        int x = Location.X + 5;
                        if (x > maxW)
                            x = maxW;
                        Location = new Point(x, Location.Y);
                    }
                    else if (_bodyType == 2)
                    {
                        int x = Location.X - 5;
                        if (x < StartX)
                            x = StartX;
                        Location = new Point(x, Location.Y);
                    }
                    if (++_curPlayIndex >= _curValue.Item2)
                        _curValue = null;
                }
            }

            base.Move(ptr);
        }
        // 0原地  1向前 2向后   3释放直线攻击  4释放范围攻击1  5释放范围攻击2    6随机换位置
        DateTime _time3 = DateTime.MinValue;
        DateTime _time4 = DateTime.MinValue;
        DateTime _time5 = DateTime.MinValue;
        DateTime _time6 = DateTime.MinValue;
        private int GetBody()
        {
            if(_time3== DateTime.MinValue)
                _time3= DateTime.Now;
            if (_time4== DateTime.MinValue)
                _time4=DateTime.Now;
            if (_time5 == DateTime.MinValue)
                _time5 = DateTime.Now;
            if (_time6 == DateTime.MinValue)
                _time6 = DateTime.Now;

            DateTime dt = DateTime.Now;
            if ((dt - _time3).TotalSeconds >= 2)
            {
                _time3 = dt;
                return 3;
            }
            if ((dt - _time4).TotalSeconds >= 4)
            {
                _time4 = dt;
                return 4;
            }
            if ((dt - _time5).TotalSeconds >= 6)
            {
                _time5 = dt;
                return 5;
            }
            if ((dt - _time6).TotalSeconds >= 3)
            {
                _time6 = dt;
                return 6;
            }
            
            var list = new List<int>(3);
            list.Add(0);
            list.Add(1);
            list.Add(2);
            var n = _random.Next(list.Count);
            return list[n];
        }
        private Tuple<int, int> GetBody(int n)
        {
            if (n == 0)
            {
                return new Tuple<int, int>(0, 3);
            }
            else if (n == 1)
            {
                return new Tuple<int, int>(4, 7);
            }
            else if (n == 2)
            {
                return new Tuple<int, int>(8, 11);
            }
            else if (n == 3)
            {
                return new Tuple<int, int>(12, 16);
            }
            else if (n == 4)
            {
                return new Tuple<int, int>(17, 21);
            }
            else if(n==5)
                return new Tuple<int, int>(22, 28);
            else
                return new Tuple<int, int>(29, 37);
        }
    }
}
